/*
 * openmodes.cpp
 *
 *  Created on: Oct 20, 2012
 *      Author: m
 */

#include <iostream>
#include <fstream>

/*
 * Programa para refrescarnos el funcionamiento de los distintos openmodes en un fstream
 */

using namespace std;

void printResult( fstream &file )
{
    if ( file.fail() )
        cout << "fallo" << endl;
    else
        cout << "ok" << endl;
};

int main ( int argc, char* argv[] )
{
    string baseFileName("archivoinexistente");
    string fileName;
    ios_base::openmode openMode;
    fstream file;

    //file.exceptions ( ifstream::failbit | ifstream::badbit );

    cout << "Pruebas sobre archivo inexistente:" << endl;

    cout << "Modo default (in|out) : ";
    fileName = baseFileName;
    fileName += "_default";
    file.open( fileName.c_str() );
    printResult( file );
    file.close();

    cout << "Modo in|out : ";
    fileName = baseFileName;
    fileName += "_in.out";
    openMode = ios_base::in | ios_base::out;
    file.open( fileName.c_str() );
    printResult( file );
    file.close();

    cout << "Modo in : ";
    fileName = baseFileName;
    fileName += "_in";
    openMode = ios_base::in;
    file.open( fileName.c_str(), openMode );
    printResult( file );
    file.close();

    cout << "Modo out : ";
    fileName = baseFileName;
    fileName += "_out";
    openMode = ios_base::out;
    file.open( fileName.c_str(), openMode );
    printResult( file );
    file.close();

    cout << "Modo trunc : ";
    fileName = baseFileName;
    fileName += "_trunc";
    openMode = ios_base::trunc;
    file.open( fileName.c_str(), openMode );
    printResult( file );
    file.close();

    cout << "Modo app : ";
    fileName = baseFileName;
    fileName += "_app";
    openMode = ios_base::app;
    file.open( fileName.c_str(), openMode );
    printResult( file );
    file.close();

    cout << "Modo ate : ";
    fileName = baseFileName;
    fileName += "_ate";
    openMode = ios_base::ate;
    file.open( fileName.c_str(), openMode );
    printResult( file );
    file.close();

    cout << "Modo trunc | out : ";
    fileName = baseFileName;
    fileName += "_trunc.out";
    openMode = ios_base::trunc | ios_base::out;
    file.open( fileName.c_str(), openMode );
    printResult( file );
    file.close();

    cout << "Modo app | out : ";
    fileName = baseFileName;
    fileName += "_app.out";
    openMode = ios_base::app  | ios_base::out;
    file.open( fileName.c_str(), openMode );
    printResult( file );
    file.close();

    cout << "Modo ate | out : ";
    fileName = baseFileName;
    fileName += "_ate.out";
    openMode = ios_base::ate  | ios_base::out;
    file.open( fileName.c_str(), openMode );
    printResult( file );
    file.close();

    return 0;
}
